package com.epam.edu.study.servlets;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.Enumeration;

/**
 * Created with IntelliJ IDEA.
 * User: Zhanna_Mukanova
 * Date: 9/24/13
 * Time: 2:01 PM
 * To change this template use File | Settings | File Templates.
 */
@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {

    //   public void service(HttpServletRequest request,
    //                     HttpServletResponse response)
    //           throws ServletException, IOException {
    //       PrintWriter writer = response.getWriter();
    //     writer.println("Hello, World!");
    //   writer.close();
    //  }
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
//        Enumeration e = request.getHeaderNames();
//        while (e.hasMoreElements()) {
//            String name = (String) e.nextElement();
//            String value = request.getHeader(name);
//            out.println(name + " = " + value);
//        }
        try {
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            String connectionUrl = "jdbc:sqlserver://localhost:1433;databaseName=studyDB;integratedSecurity=true;";
            Connection con = DriverManager.getConnection(connectionUrl);
            out.println("OK");
        } catch (Exception e1) {
            out.println(e1.getMessage());
        }


    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // установка MIME-типа содержания ответа
        response.setContentType("text/html");
// поток для данных ответа
        PrintWriter out = response.getWriter();
        out.println("<HTML><HEAD>");
        out.println("<TITLE>Request Information </TITLE>");
        out.println("</HEAD><BODY>");
        out.println("<H3>Request Information </H3>");
        out.println("<BR>Method: " + request.getMethod());
        out.println("<BR>Request URI: " +
                request.getRequestURI());
        out.println("<BR>Protocol: " +
                request.getProtocol());
        out.println("<BR>PathInfo: " +
                request.getPathInfo());
        out.println("<BR>Remote Address: " +
                request.getRemoteAddr());
        out.println("</BODY></HTML>");
// закрытие потока
        out.close();
    }
}
